﻿using System;
using System.Collections.Generic;

namespace ReactiveGraph.Core.Extensions
{
    public static class MemoizationExtensions
    {
        public static Func<T, U> Memoize<T, U>(this Func<T, U> f)
        {
            var map = new Dictionary<T, U>();
            return a =>
                       {
                           U value;
                           if (map.TryGetValue(a, out value))
                               return value;
                           value = f(a);
                           map.Add(a, value);
                           return value;
                       };
        }
    }
}